package com.example.networktest;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

// SAX 解析方式
public class ContentHandler extends DefaultHandler {

    private String nodeName;

    private  StringBuilder id;

    private StringBuilder name;

    private StringBuilder version;

    @Override
    public void startDocument() throws SAXException{
        id = new StringBuilder();
        name = new StringBuilder();
        version = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
        nodeName = localName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException{ //获取节点内容调用
        // 根据当前的节点名判断将内容添加到哪一个 StringBuilder 对象中
        if("id".equals(nodeName)){
            id.append(cd ,start,length);
        } else if("name".equals(nodeName)){
            name.append(ch,start,length);
        } else if("version".equals(nodeName)){
            version.append(ch,start,length);
        }
    }

    @Override
    public void endElement(String uri,String localName,String gName) throws SAXException{
        if("app".equals(localName)){
            Lod.d("ContentHandler","id is" + id.toString().trim());
            Lod.d("ContentHandler","name is" + name.toString().trim());
            Lod.d("ContentHandler","version is" + version.toString().trim());

            // 最后要将 StringBuilder 清空掉
            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }
    }

    @Override
    public void endDocument() throws SAXException{
        super.endDocument();
    }

}
